package com.chuanyang.driverh.mainorder.fragment;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.amap.MapUtils;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.GoodsAssignWeightNumActivity;
import com.chuanyang.driverh.mainorder.NewOrderDetailActivity;
import com.chuanyang.driverh.mainorder.OrderLocationActivity;
import com.chuanyang.driverh.mainorder.SureUnLoadOverActivity;
import com.chuanyang.driverh.mainother.BeforeNaviActivity;
import com.chuanyang.driverh.responseresult.CarCurrentStatusResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.OrderAddressResult;
import com.chuanyang.driverh.responseresult.OrderData;
import com.chuanyang.driverh.responseresult.OrderResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ValidateCodeResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.eventbus.RefleshDriverInfoEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 我的订单
 */
public class OrderFragment extends BaseListFragment {
    private MyOrderAdapter orderAdaoter;
    private DriverInfoData driverinfo;
    private OrderData.DataBean bean;
    private AlertDialog alertDialog;

    private EditText et_sureunload_code;
    private TextView tv_cancle_unload;
    private TextView tv_sureunload_getcode;
    private TextView tv_sure_unload;

    private boolean validateOk;
//    private boolean hasInitData ;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://确认收货
                    OrderData.DataBean orderData = (OrderData.DataBean) msg.obj;
                    if (orderData.getFleetId() != null) {
                        updateWaybillUnload(orderData);
                    } else {
                        updateOrderUnload(orderData);
                    }
                    break;
            }
        }
    };
    private ArrayList<File> files;

    @Override
    public BaseListAdapter createAdapter() {
        return orderAdaoter = new MyOrderAdapter(getActivity());
    }

    @Override
    protected void initData() {
        refreshDriverInfo();
    }
    @Override
    protected void initListener() {
    }

 /*   @Override
    public void onResume() {
        super.onResume();
        if (!hasGetData)return;
        refreshDriverInfo();
    }*/

    public void refreshDriverInfo() {
        driverinfo = BaseApplication.getInstance().getDriverInfo();
        if (driverinfo==null)return;
        if (driverinfo.getDriver() != null && driverinfo.getDriver().getCarId() != 0) {
            GetCarCurrentStatusByCarID(driverinfo.getDriver().getCarId() + "");
        } else {
            GetCarCurrentStatusByCarID("");
        }
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
//        BaseApplication.getInstance().getDriverInfoFromNet();
    }
    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
        onRefresh();
//        BaseApplication.getInstance().getDriverInfoFromNet();
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        OrderData.DataBean order = (OrderData.DataBean) orderAdaoter.getItem(position);
        //  startActivity(OrderDetailActivity.class, order.getOrderId() + "");
        startActivity(NewOrderDetailActivity.class, order.getOrderId() + "");
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }


    /**
     * 其它操作比如在详情页操作订单，导致订单变化，此时需要刷新订单列表
     * @param event
     */
   /* public void onEventMainThread(RefleshOrderData event) {
        if (event.getIndex() == 1) {//1刷新订单列表  2刷新运单列表
            onRefresh();
        }
    }
*/
    public void onEventMainThread(RefleshDriverInfoEvent event) {
        refreshDriverInfo();
    }


    /**
     * 提示司机输入收货人验证码
     */
    public void sureAcceptGood(final OrderData.DataBean dataBean) {

        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(getActivity()).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_sureunload);
            View view = window.getDecorView();
            et_sureunload_code = (EditText) view.findViewById(R.id.et_sureunload_code);
            tv_cancle_unload = (TextView) view.findViewById(R.id.tv_cancle_unload);
            tv_sureunload_getcode = (TextView) view.findViewById(R.id.tv_sureunload_getcode);
            tv_sure_unload = (TextView) view.findViewById(R.id.tv_sure_unload);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    validateOk = false;

                }
            });
            tv_cancle_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sureunload_getcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!validateOk) {//获取成功验证码 且对话框没消失时无法多次点击
                        getValidateCode(dataBean);
                    } else {
                        PrintUtil.toast(getActivity(),"验证码已发送给货主！");
                    }
                }
            });
            tv_sure_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et_sureunload_code.getText().toString().trim())) {
                        PrintUtil.toast(getActivity(),"请填写获取到验证码");
                        return;
                    }
                    if (!validateOk){
                        PrintUtil.toast(activityContext,"请先获取验证码");
                        return;
                    }
                    Message message = Message.obtain();
                    message.obj = dataBean;
                    message.what = 1;
                    mHandler.sendMessage(message);
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
        }
        //下面两行代码加入后即可弹出输入法
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    private void getValidateCode(OrderData.DataBean bean) {
        DialogUtil.showLoadingDialog(getActivity(),"验证码...");
        Map<String,String> params=new HashMap<String, String>();
        validateOk = false;
        params.put("phone", bean.getContactPhone());
        params.put("validateMsg", bean.getOrderId() + "");
        MyHttpUtil.getSureValidateCode(getActivity(),params, new MyCallback<ValidateCodeResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ValidateCodeResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    validateOk = true;
                    tv_sureunload_getcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.tv_shape_gray_round_corner_solid));
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }

            }
        });

    }

    /**
     * 收货完成 （传入订单id或者运单id）
     */
    private void updateWaybillUnload(OrderData.DataBean dataBean) {
        DialogUtil.showLoadingDialog(getActivity(),"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("orderId", dataBean.getOrderId() + "");
        params.put("waybillStatus", "7");
        params.put("validateMsg", dataBean.getOrderId() + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateWaybillStatus(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"您已替货主确认收货");
                    mCurrentPage = 1;
                    getData();
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }







    /**
     * 代替货主收货接口 (个人订单时)
     */
    private void updateOrderUnload(OrderData.DataBean dataBean) {
        DialogUtil.showLoadingDialog(getActivity(),"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        if (files == null) {
            files = new ArrayList();
        }
        params.clear();
        params.put("orderId", dataBean.getOrderId() + "");
        params.put("orderStatus", "7");
        params.put("validateMsg", dataBean.getOrderId() + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateOrderUnload(getActivity(),files,params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"收货成功！");
                    mCurrentPage = 1;
                    getData();
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    /**
     * 获取订单或者运单的列表
     */
    private void getData() {
        getEmptyView().setVisibility(View.GONE);
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        if (files == null) {
            files = new ArrayList();
        }
        params.clear();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        MyHttpUtil.selectdriverOrder(getActivity(),params, new MyCallback<OrderResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id){
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                onShowFailed();
            }
            @Override
            public void onSuccess(OrderResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setDataResult(result.getData().getData());
//                    hasInitData = true;
                }else{
                    onShowNull();
                    springView.onFinishFreshAndLoad();
//                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    private void selectOrderAddress(String id) {
        getEmptyView().setVisibility(View.GONE);
        DialogUtil.showLoadingDialog(getActivity(),"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("orderId", id);
        MyHttpUtil.selectOrderAddress(getActivity(),params, new MyCallback<OrderAddressResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }
            @Override
            public void onSuccess(OrderAddressResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    Intent intent = new Intent(getActivity(), BeforeNaviActivity.class);
                    intent.putExtra("OrderAddressResult", result);
                    startActivity(intent);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
    /**
     * 通过车辆id获取车辆位置
     * CarID
     *
     * @param
     */
    private void GetCarCurrentStatusByCarID(String carId) {
        Map<String,String> params=new HashMap<String, String>();
        params.put("CarID", carId);
        MyHttpUtil.GetCarCurrentStatusByCarID(getActivity(),params, new MyCallback<CarCurrentStatusResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                onRefresh();
            }
            @Override
            public void onSuccess(CarCurrentStatusResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData() != null && result.getData().size() > 0) {
                        LatLng targetLatLng = MapUtils.getGdLatLng(getActivity(), result.getData().get(0).getLast_lat(), result.getData().get(0).getLast_lon());
                        SharedPrefUtil.setLongitude(targetLatLng.longitude + "");
                        SharedPrefUtil.setLatitude(targetLatLng.latitude + "");
                    }
                }
                onRefresh();
            }
        });
    }

    /**
     * 修改订单状态
     */
    private void startTranslate(final OrderData.DataBean order, final int position) {
        DialogUtil.showLoadingDialog(getActivity(),"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("waybillId", order.getWaybillSimpleMsg().getWaybillId()+"");
        params.put("waybillStatus", "4");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.startTranslate(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    order.setOrder_status(4);
                    mAdapter.replaceOnedata(position, order);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }

    /**
     * 车队长点击订单完成按钮，则订单变为卸货完成按钮
     */
    private void updateOrderStatusByWaybill(final OrderData.DataBean order, final int position) {
        DialogUtil.showLoadingDialog(getActivity(),"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("orderId", order.getOrderId() + "");
        params.put("orderStatus", "6");
        MyHttpUtil.updateOrderStatusByWaybill(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    order.setOrder_status(6);
                    mAdapter.replaceOnedata(position, order);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }






    private class MyOrderAdapter extends BaseListAdapter {
        private OrderFragment.MyOrderAdapter.ViewHolder viewHolder;
        public MyOrderAdapter(Context context) {
            super(context);
        }
        @Override
        public View getZkView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getInflater().inflate(R.layout.common_item_myorder, parent, false);
                viewHolder = new OrderFragment.MyOrderAdapter.ViewHolder();
                viewHolder.ll_driverbill_account = (LinearLayout) convertView.findViewById(R.id.ll_driverbill_account);
                viewHolder.iv_myorder_image = (ImageView) convertView.findViewById(R.id.iv_myorder_image);
                viewHolder.iv_myorder_fleedpersonal = (ImageView) convertView.findViewById(R.id.iv_myorder_fleedpersonal);//车队订单是个人订单
                viewHolder.rb_stars = (RatingBar) convertView.findViewById(R.id.rb_stars);
                viewHolder.tv_myorder_ordertype = (TextView) convertView.findViewById(R.id.tv_myorder_ordertype);
                viewHolder.tv_myorder_tradetype = (TextView) convertView.findViewById(R.id.tv_myorder_tradetype);//订单外贸类型（1人民币，2美元）
                viewHolder.tv_myorder_name = (TextView) convertView.findViewById(R.id.tv_myorder_name);
                viewHolder.tv_myorder_type = (TextView) convertView.findViewById(R.id.tv_myorder_type);
                viewHolder.tv_myorder_startadd = (TextView) convertView.findViewById(R.id.tv_myorder_startadd);
                viewHolder.tv_myorder_endadd = (TextView) convertView.findViewById(R.id.tv_myorder_endadd);
                viewHolder.tv_myorder_starttim = (TextView) convertView.findViewById(R.id.tv_myorder_starttim);
                viewHolder.tv_myorder_endtim = (TextView) convertView.findViewById(R.id.tv_myorder_endtim);
                viewHolder.tv_myorder_carshipstarttim = (TextView) convertView.findViewById(R.id.tv_myorder_carshipstarttim);
                viewHolder.tv_myorder_carshipendtim = (TextView) convertView.findViewById(R.id.tv_myorder_carshipendtim);
                viewHolder.tv_myorder_num = (TextView) convertView.findViewById(R.id.tv_myorder_num);
                viewHolder.tv_myorder_createtime = (TextView) convertView.findViewById(R.id.tv_myorder_createtime);
                viewHolder.tv_myorder_load = (TextView) convertView.findViewById(R.id.tv_myorder_load);
                viewHolder.tv_myorder_sum = (TextView) convertView.findViewById(R.id.tv_myorder_sum);
                viewHolder.tv_myorder_goodname = (TextView) convertView.findViewById(R.id.tv_myorder_goodname);
                viewHolder.tv_myorder_amount = (TextView) convertView.findViewById(R.id.tv_myorder_amount);
                viewHolder.tv_myorder_status = (TextView) convertView.findViewById(R.id.tv_myorder_status);//订单状态
                viewHolder.bt_myorder_act1 = (TextView) convertView.findViewById(R.id.bt_myorder_act1);
                viewHolder.bt_myorder_act2 = (TextView) convertView.findViewById(R.id.bt_myorder_act2);
                viewHolder.bt_myorder_act3 = (TextView) convertView.findViewById(R.id.bt_myorder_act3);
                viewHolder.tv_myorder_orginamount = (TextView) convertView.findViewById(R.id.tv_myorder_orginamount);//下单金额
                viewHolder.tv_myorder_orginload = (TextView) convertView.findViewById(R.id.tv_myorder_orginload);//下单重量
                viewHolder.tv_myorder_orginsum = (TextView) convertView.findViewById(R.id.tv_myorder_orginsum);//下单数量
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (OrderFragment.MyOrderAdapter.ViewHolder) convertView.getTag();
            }
            viewHolder.bt_myorder_act3.setVisibility(View.GONE);//初始化时最左侧按钮按钮不可见
            viewHolder.bt_myorder_act2.setVisibility(View.GONE);//初始化时第三个按钮不可见
            viewHolder.bt_myorder_act1.setVisibility(View.GONE);//初始化时第二个按钮不可见
            viewHolder.tv_myorder_status.setVisibility(View.GONE);
            bean = (OrderData.DataBean) getItem(position);
            viewHolder.ll_driverbill_account.setVisibility(View.VISIBLE);
            viewHolder.tv_myorder_type.setText("订单号");
            BitmapUtil.showImgForNetPath( bean.getUserImage(),viewHolder.iv_myorder_image);
            viewHolder.tv_myorder_name.setText(bean.getUsername());
            viewHolder.tv_myorder_startadd.setText(StringUtil.getSimpleMunicipality(bean.getStartAddr()));
            viewHolder.tv_myorder_endadd.setText(StringUtil.getSimpleMunicipality(bean.getTargetAddr()));

            viewHolder.tv_myorder_num.setText(bean.getOrderNo());
            viewHolder.tv_myorder_createtime.setText(TimeUtils.getStandardTime(bean.getOrder_time()));
            if (bean.getCurrency() == 1) {
                viewHolder.tv_myorder_tradetype.setVisibility(View.GONE);
            } else {
                viewHolder.tv_myorder_tradetype.setVisibility(View.VISIBLE);

            }
            if (bean.getRealitySum() != null) {
                viewHolder.tv_myorder_load.setText(bean.getRealitySum().getRealityWeight() + "t");
                viewHolder.tv_myorder_sum.setText(bean.getRealitySum().getRealityNum() + "");
                double realamount = (bean.getRealitySum().getRealityWeight()) * (bean.getPrice());
                //根据金额是否有金额确认显示与否
                viewHolder.tv_myorder_amount.setText(String.format("%.2f", realamount) + bean.getUnit());
            } else {
                viewHolder.tv_myorder_load.setText("---");
                viewHolder.tv_myorder_sum.setText("---");
                viewHolder.tv_myorder_amount.setText("---");

            }
            if (bean.getGoodsSum() != null) {
                viewHolder.tv_myorder_orginload.setText("/" + bean.getGoodsSum().getGoodsWeight() + "t");
                viewHolder.tv_myorder_orginsum.setText("/" + bean.getGoodsSum().getGoodsNum() + "");
                double orginamount = (bean.getGoodsSum().getGoodsWeight()) * (bean.getPrice());
                viewHolder.tv_myorder_orginamount.setText("挂牌：" +String.format("%.2f", orginamount) + bean.getUnit());
            }
            else {
                viewHolder.tv_myorder_load.setText("/---");
                viewHolder.tv_myorder_sum.setText("/---");
                viewHolder.tv_myorder_amount.setText("挂牌："+"---");

            }

            setGoodName(viewHolder.tv_myorder_goodname, bean.getGoodsName());//货品名称
            if (bean.getTransPortWay() == 1)//车
            {
                viewHolder.tv_myorder_starttim.setText(TimeUtils.getStandardTime(bean.getOrder_startime()));
                viewHolder.tv_myorder_endtim.setText(TimeUtils.getStandardTime(bean.getOrder_endtime()));
                viewHolder.tv_myorder_carshipstarttim.setText("接货:");
                viewHolder.tv_myorder_carshipendtim.setText("卸货:");
                if (bean.getFleetId() == null) {
                    viewHolder.iv_myorder_fleedpersonal.setImageResource(R.mipmap.car_transport1);
                } else {
                    viewHolder.iv_myorder_fleedpersonal.setImageResource(R.mipmap.car_transport0);

                }
            } else if (bean.getTransPortWay() == 2)//船   ll_driverbill_account
            {
                viewHolder.tv_myorder_starttim.setText(bean.getLoadDate());
                viewHolder.tv_myorder_endtim.setText(bean.getStevedoreDay());
                viewHolder.tv_myorder_carshipstarttim.setText("受载日期:");
                viewHolder.tv_myorder_carshipendtim.setText("装卸期限:");
                if (bean.getFleetId() == null) {
                    viewHolder.iv_myorder_fleedpersonal.setImageResource(R.mipmap.shipping1);
                } else {
                    viewHolder.iv_myorder_fleedpersonal.setImageResource(R.mipmap.shipping0);

                }
            }
            if (bean.getStar() != null) {
                viewHolder.rb_stars.setRating(bean.getStar());

            }

            //根据订单类型确定显示
            if (bean.getOrder_type().equals(1))//竞价
            {
                viewHolder.tv_myorder_ordertype.setText("竞价");
                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_compete);
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.compete));
            } else if (bean.getOrder_type().equals(2))//匹配
            {
                viewHolder.tv_myorder_ordertype.setText("匹配");
                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_macher);
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.match));

            } else {//专线
                viewHolder.tv_myorder_ordertype.setText("专线");
                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_special);
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.specialline));


            }

            if (bean.getOrder_status() < 0) {
                viewHolder.tv_myorder_status.setVisibility(View.VISIBLE);
                viewHolder.tv_myorder_status.setText("正在处理");


            } else if (bean.getOrder_status() == 1) {//这个必定是车队订单
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("部署");
                }


            } else if (bean.getOrder_status() == 2) {
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act1.setText("查看运单");
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("继续部署");
                    viewHolder.bt_myorder_act3.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act3.setText("完成订单");

                } else {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("开始运输");
                    viewHolder.tv_myorder_status.setText("待装货");
                    viewHolder.tv_myorder_status.setVisibility(View.VISIBLE);

                }


            } else if (bean.getOrder_status() == 3) {
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act1.setText("查看运单");
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("继续部署");
                    viewHolder.bt_myorder_act3.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act3.setText("完成订单");
                } else {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("装货向导");
                    viewHolder.tv_myorder_status.setText("装货中");
                    viewHolder.tv_myorder_status.setVisibility(View.VISIBLE);

                }

            } else if (bean.getOrder_status() == 4) {
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act1.setText("查看运单");
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("继续部署");
                    viewHolder.bt_myorder_act3.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act3.setText("完成订单");

                } else //此时为个人订单
                {

                    //  if (showUnload(bean.getLatAndLng())) {
                    //如果是车显示导航 如果是船则隐藏
                    if (bean.getTransPortWay() == 1)//车
                    {
                        viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                        viewHolder.bt_myorder_act1.setText("导航");
                    } else if (bean.getTransPortWay() == 2) {
                        viewHolder.bt_myorder_act1.setVisibility(View.GONE);

                    }
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("运输完成");
                    viewHolder.tv_myorder_status.setText("运输中");
                    viewHolder.tv_myorder_status.setVisibility(View.VISIBLE);

//                        } else {
//                            viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
//                            viewHolder.bt_myorder_act1.setText("导航");
//                        }
                }


            } else if (bean.getOrder_status() == 5) {
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act1.setText("查看运单");
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("部署");
                    viewHolder.bt_myorder_act3.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act3.setText("完成订单");

                } else {

                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("卸货向导");
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("运输完成");
                    viewHolder.tv_myorder_status.setText("卸货中");
                    viewHolder.tv_myorder_status.setVisibility(View.VISIBLE);

                }


            } else if (bean.getOrder_status() == 6) {
                if (bean.getFleetId() != null) {
                    viewHolder.bt_myorder_act1.setText("查看运单");
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);

                } else {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("验证卸货");
                    viewHolder.tv_myorder_status.setText("卸货完成，等待验证卸货");

                }

            }


            viewHolder.bt_myorder_act1.setOnClickListener(new View.OnClickListener() {//由于车队订单只有状态1时显示，所以点击其按钮只需要判断其订单状态，无需判断是否是车队了
                @Override
                public void onClick(View v) {
                    bean = (OrderData.DataBean) getItem(position);
                    if (bean.getFleetId() != null) {//车队订单
                        Intent intent = new Intent(getActivity(), OrderLocationActivity.class);
                        intent.putExtra("orderId", bean.getOrderId() + "");
                        intent.putExtra("orderNo", bean.getOrderNo() + "");
                        startActivity(intent);
                    } else {
                        if (bean.getOrder_status() == 2) {
                            //个人订单状态为2  点击其状态修改为4(车队订单此时没有按钮)
                            startTranslate(bean, position);
                        } else if (bean.getOrder_status() == 3) {
                            PrintUtil.toast(getActivity(),"卸货向导");

                        } else if (bean.getOrder_status() == 4) {
                            //  showMsg("导航");
                            //  requsetOrderData(bean);
                            selectOrderAddress(bean.getOrderId() + "");
                        } else if (bean.getOrder_status() == 5) {
                            PrintUtil.toast(getActivity(),"卸货向导");
                        } else if (bean.getOrder_status() == 6) {
                            sureAcceptGood(bean);
                        }
                    }
                }
            });
            viewHolder.bt_myorder_act2.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                              bean = (OrderData.DataBean) getItem(position);
                              if (bean.getFleetId() != null) {
//                                  startActivity(AssignGoodsActivity.class, bean.getOrderId() + "");
//                                  startActivity(GoodsAssignActivity.class, bean.getOrderId() + "");
                                  startActivity(GoodsAssignWeightNumActivity.class, bean.getOrderId() + "");


                              } else {
                                  Intent intent = new Intent(getActivity(), SureUnLoadOverActivity.class);
                                  //  intent.putExtra("phone", bean.getPhone());
                                  intent.putExtra("orederId", bean.getOrderId());
                                  intent.putExtra("fromlist", true);
                                  intent.putExtra("personalOrder", true);
                                  startActivity(intent);
                              }
                          }
                      }
            );
            viewHolder.bt_myorder_act3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bean = (OrderData.DataBean) getItem(position);
                    //车队长完成订单
                    updateOrderStatusByWaybill(bean, position);
                }
            });
            return convertView;
        }
        private void setGoodName(TextView view, List<String> goodname) {
            String name = StringUtil.getString(goodname);
            view.setText(name);
        }
        class ViewHolder {
            LinearLayout ll_driverbill_account;
            ImageView iv_myorder_image;
            ImageView iv_myorder_fleedpersonal;//是个人的订单还是车队的订单
            TextView tv_myorder_name;
            TextView tv_myorder_type;//运单还是订单
            TextView tv_myorder_startadd;
            // TextView  tv_myorder_passadd;
            TextView tv_myorder_status;//订单状态
            TextView tv_myorder_endadd;
            TextView tv_myorder_starttim;
            TextView tv_myorder_endtim;
            TextView tv_myorder_carshipstarttim;//接货  受载日期
            TextView tv_myorder_carshipendtim;//卸货  装载期限
            TextView tv_myorder_num;
            TextView tv_myorder_createtime;
            TextView tv_myorder_load;
            TextView tv_myorder_sum;//数量
            RatingBar rb_stars;
            TextView tv_myorder_ordertype;//订单类型
            TextView tv_myorder_goodname;
            TextView tv_myorder_amount;
            TextView tv_myorder_orginamount;//下单金额
            TextView tv_myorder_orginload;//实际载重
            TextView tv_myorder_orginsum;//实际数量
            TextView tv_myorder_time;
            TextView bt_myorder_act1;
            TextView bt_myorder_act2;
            TextView bt_myorder_act3;//最左侧完成订单按钮
            TextView tv_myorder_tradetype;//订单外贸类型（1人民币，2美元）
        }
    }
}
